<template>
  <div>
    <el-dialog
      :title="dialogTitle"
      :visible="visible"
      width="50%"
      @close="onDialogClose"
    >
      <el-form
        ref="form"
        :model="addDeptForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="addDeptForm.name" placeholder="1-10位" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="addDeptForm.code" placeholder="1-10位" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="addDeptForm.manager" placeholder="请选择" @focus="getManager">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.username"
              :value="item.username"
            />
          </el-select>
          <!-- <el-input v-model="addDeptForm.manager" placeholder="1-10位" /> -->
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="addDeptForm.introduce" placeholder="1-50位" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('input', false)">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeesSimpleList } from '@/api/employees'
import { getDepartmentsList, addDepartment, getNewDepartmentDataById, changeDepartmentDataById } from '@/api/departments'
export default {
  model: {
    prop: 'visible'
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    // 当前数据
    node: {
      type: Object,
      required: true
    }
  },
  data() {
    const validateName = async(rule, value, callback) => {
      const res = await getDepartmentsList()
      let flag = false
      // 编辑时
      if (this.addDeptForm.id) {
        const newArr = res.depts.filter(item => item.pid === this.node.pid && item.id !== this.node.id)
        flag = newArr.some(item => item.name === value)
      } else {
        // 新增时
        if (!this.node.children) return callback()
        flag = this.node.children.some(item => item.name === value)
      }
      // 根据flag判断
      if (flag) {
        callback(new Error('不允许设置' + value))
      } else {
        callback()
      }
    }
    const validateCode = async(rule, value, callback) => {
      const res = await getDepartmentsList()
      let flag = false
      // 编辑时
      if (this.addDeptForm.id) {
        // 首先过滤‘与编辑时存的编码相同’的编码
        const newArr = res.depts.filter(item => item.code !== this.node.code)
        flag = newArr.some(item => item.code === value)
      } else {
        // 新增时
        flag = res.depts.some(item => item.code === value)
      }
      if (flag)callback(new Error('不允许部门编码设置' + value))
      callback()
    }
    return {
      addDeptForm: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '部门名称1-10位', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 10, message: '部门编码1-10位', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }

        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 50, message: '部门介绍1-50位', trigger: 'blur' }
        ]
      },
      options: []
    }
  },
  computed: {
    dialogTitle() {
      return this.addDeptForm.id ? '编辑部门' : '新增部门'
    }
  },
  created() {

  },

  methods: {
    onDialogClose() {
      this.$emit('input', false)
      // 重置表单
      this.$refs.form.resetFields()
      // 清空vuex内存储的表单
      this.addDeptForm = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    async getManager() {
      const res = await getEmployeesSimpleList()
      this.options = res
    },
    async onSave() {
      try {
        await this.$refs.form.validate()
        if (this.addDeptForm.id) {
        // 编辑
          await changeDepartmentDataById(this.addDeptForm)
          this.$message.success('编辑成功')
          this.$emit('edit-success')
        } else {
        // 新增
          this.addDeptForm.pid = this.node.id
          await addDepartment(this.addDeptForm)
          this.$message.success('添加成功')
          this.$emit('add-success')
        }
      } catch (err) {
        console.log(err)
      }
      this.onDialogClose()
    },
    // 获取最新部门数据
    async getNewDepartmentData(id) {
      // console.log('最新' + id)
      console.log(id)
      const res = await getNewDepartmentDataById(id)
      this.addDeptForm = res
    }
  }
}
</script>

<style scoped lang='less'>

</style>
